<?php

ini_set("display_errors", "1");
error_reporting(E_ALL);
@include_once("Classes/Dealer.php");
@include_once("Classes/Store.php");
@include_once("Classes/TT.php");
@include_once("Classes/TTStatusChange.php");
tts_connectDB();
if (!session_id()) {
    session_start();
}

@$store = new Store($_SESSION['storeAbbrev']);
@$dealer = new Dealer($_SESSION['dealerCode']);

function tts_connectDB() {
    $db_name = "tts";
// Connect to server and select databse.
//mysql_connect('cellularsales-sg.ipowermysql.com', 'jamesweston13', 'liveLife13') or die("Couldn't connect in mysqlConnector.php");
mysql_connect('localhost', 'root', '') or die("Couldn't connect in mysqlConnector.php");
    if (mysqli_connect_errno ()) {
        echo ' Error no connection';
        exit;
    }
    mysql_select_db("$db_name") or die("Couldn't connct to DB in mysqlConnector.php");
}

function tts_getNextStatus($currentStatus) {
    $statusArr = array('Submitted', 'Read', 'In Progress', 'Resolved');
    $next = 0;
    for ($i = 0; $i < sizeof($statusArr); $i++) {
        if ($currentStatus == $statusArr[$i]) {
            $next = ++$i;
            break;
        }
    }
    return $statusArr[$next];
}

function tts_DateTimeNowSpoken() {
    return ("F j, Y g:i A");
}

function tts_DateTimeNow() {
    return date("m/d/Y g:i A");
}

function tts_PriorityDateTime($hours) {
    $currentTime = time(tts_DateTimeNow());
    $timeAfterOneHour = $currentTime + 60 * 60 * $hours;
    return date("m/d/Y g:i A", $timeAfterOneHour);
}

function tts_SendText($to, $subject, $message) {
    return mail($to . '@vtext.com', $subject, $message, 'From: TTS');
}

function tts_EmailAdmin($subject, $message) {
    mail('james.weston@cellularsales.com', $subject, $message);
}

function tts_getAllTTOfStatus($status, $dealerCode) {
    $query = 'select id from tt where currentStatus = \'' . $status . '\'
        AND submittedBy=\'' . $dealerCode . '\';';
    $result = mysql_query($query) or die('query failed in tts_getAllTTOfStatus()<br/>' . $query);
    $tts = array();
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        array_push($tts, (new TT($row['id'])));
    }
    return $tts;
}

function tts_getTicketHeader() {
    echo "<table class=\"maintable\">";
    echo "<tr>
        <td class=\"tableheader\">TT ID</td>
        <td class=\"tableheader\">Submitted By</td>
        <td class=\"tableheader\">Assigned To</td>
        <td class=\"tableheader\">Type</td>
        <td class=\"tableheader\">Time Submitted</td>
        <td class=\"tableheader\">Time To Resolve</td>
        <td class=\"tableheader\">Description</td>
        <td class=\"tableheader\">PC</td>
        <td class=\"tableheader\">Status</td>
        </tr>";
}

function tts_getTicketTR($ttid) {
    $tt = new TT($ttid);
    $assignedDealer = new Dealer($tt->assignedTo);
    echo "<tr>";
    echo "<td><a href=showTickets_history.php?ttid=" . $tt->id . ">" . $tt->id . "</a></td>";
    echo "<td>" . $tt->submittedByName . "</td>";
    echo "<td>" . $assignedDealer->fullName . "</td>";
    echo "<td>" . $tt->type . "</td>";
    echo "<td>" . $tt->dateTimeSubmitted . "</td>";
    echo "<td>" . $tt->dateTimeFinished . "</td>";
    echo "<td>" . $tt->description . "</td>";
    if ($tt->specialID == '' || $tt->specialID == null) {
        echo "<td>N/A</td>";
    } else {
        echo "<td>" . $tt->specialID . "</td>";
    }
    echo "<td>" . $tt->currentStatus . "</td>";
    echo "</tr>";
}

function tts_getheader() {
    $store = new Store($_SESSION['storeAbbrev']);
    $dealer = new Dealer($_SESSION['dealerCode']);

    echo "<p id=\"dealer\">Dealer: " . $dealer->firstName . " " . $dealer->lastName . " (" . $dealer->dealerCode . ")</p>";
    echo "<table class=\"maintable\">
    <tr>
        <td class=\"tableheader\">Your Market</td>
        <td class=\"tableheader\">Selected State</td>
        <td class=\"tableheader\">Selected Store</td>
        <td class=\"tableheader\">View My TT</td>";
    if ($dealer->isMA) {
        echo "<td class=\"tableheader\">TT Assigned To Me</td>";
    }
    echo "</tr>";



    echo '<tr><td>SG</td>
      <td><div id="stateDiv"><select name="state" id="state" value="GA" onchange="stateChange(this.value)">';
    $query = 'select distinct state from stores where market=\'SG\';';
    $result = mysql_query($query) or die("Query failed when selecting unique states in main.php");
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $currentState = $row['state'];
        if ($currentState == $store->state) {
            echo "<option value = \"$currentState\" selected=\"$currentState\">$currentState</option>";
        } else {
            echo "<option value = \"$currentState\">$currentState</option>";
        }
    }

    echo '<select></div></td>
                     <td><div id="storeDiv"><select name="storeAbbrev" id="storeAbbrev"
                        onchange="storeAbbrevChange(this.value)">';
    $query = 'select storeAbbrev from stores where state=\'' . $store->state . '\';';
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $strAbbrev = $row['storeAbbrev'];
        // if here
        if ($strAbbrev == $store->storeAbbrev) {
            echo "<option value = \" $ \" selected = \"$strAbbrev\" >$strAbbrev</option>";
        } else {
            echo "<option value = \"$strAbbrev\">$strAbbrev</option>";
        }
    }
    echo '</select></div></td>';
    echo '<td>';
    echo '<select id="myTTCombo" onChange="myTTChange()">';
    $statusArr = array('--Select One--', 'Submitted', 'Read', 'In Progress', 'Resolved');
    foreach ($statusArr as $status) {
        if ($status == '--Select One--') {
            echo "<option value= \"$status\">" . $status . "</option>";
        } else {
            echo "<option value= \"$status\">" . $status . ' (' . $dealer->getTTStatusCount($status) . ")</option>";
        }
    }
    echo '</select></td>';
    
    if ($dealer->isMA) {
        echo '<td>';
        echo '<select id="assignedTTCombo" onChange="assignedTTChange()">';
        $statusArr = array('--Select One--', 'Submitted', 'Read', 'In Progress', 'Resolved');
        foreach ($statusArr as $status) {
            if ($status == '--Select One--') {
                echo "<option value= \"$status\">" . $status . "</option>";
            } else {
                $query = 'select count(id) from tt where assignedTo =\'' . $dealer->dealerCode . '\' and currentStatus = \'' . $status . '\'';
                $result = mysql_query($query) or die('getHeader.php: Failed while getting count of assigned tickets');
                $row = mysql_fetch_array($result, MYSQL_ASSOC);
                $count = $row['count(id)'];
                echo "<option value= \"$status\">" . $status . ' (' . $count . ")</option>";
            }
        }
        echo '</select></td>';
    }
    echo '</table>';
}

function tts_updateStatus($newStatus, $tt, $dealerCode, $notes) {
        if ($newStatus == $tt->currentStatus) {
            echo 'TT.php.updateStatus: newStatus == currentStatus';
            return;
        }
        // Update the tt record
        $query = 'update tt set currentStatus = \'' . $newStatus . '\' where id=' . $tt->id;
        mysql_query($query) or die('failed while updating tt.currentStatus in TT.php');
        // create new ttstatus1m
        $query = 'insert into ttstatus1m values(' . $tt->id . ', null );';
        mysql_query($query) or die('failed while inserting ttstatus1m record in TT.php');
        $id = mysql_insert_id();
        // create new ttstatuschange
        $query = 'insert into ttstatuschange values(' . $id . ', \'' . date("Y-m-d H:i:s") . '\', \'' . $dealerCode
                . '\', \'' . $notes . '\', \'' . $newStatus . '\'  );';
        mysql_query($query) or die('failed while inserting ttstatuschange');
        $tt->currentStatus = $newStatus;
    }
?>
